home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / tests / chmod.test < prev    next >
Encoding:
Text File  |  1994-01-23  |  11.4 KB  |  421 lines

  1. #
  2. # chmod.test
  3. #
  4. # Tests for the chmod, chown and chgrp commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: chmod.test,v 3.0 1993/11/19 06:57:04 markd Rel $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. #-----------------------------------------------------------------------------
  22. # This routine to the the mode of a file.  It is returned formated in octal.
  23.  
  24. proc GetMode {filename} {
  25.     file stat $filename stat
  26.     return [format "%o" [expr {$stat(mode) & 07777}]]
  27. }
  28.  
  29. #-----------------------------------------------------------------------------
  30. # Certain Unix systems don't handle chmod the same.  This routine test if the
  31. # system chmod produces the expected results.
  32. #   o mode - symbolic mode to set the file to.
  33. #   o args - list of valid expected result from ls.
  34. #
  35. proc CheckChmod {mode args} {
  36.     global chmodTestsSkipped
  37.  
  38.     set stat 0
  39.     if {[catch {
  40.         chmod 000 CHECK.TMP
  41.         exec chmod $mode CHECK.TMP
  42.         set sysMode [lindex [exec ls -l CHECK.TMP] 0]
  43.     }] == 0} {
  44.         if {[lsearch -exact $args $sysMode] >= 0} {
  45.             set stat 1
  46.         }
  47.     }
  48.     if !$stat {
  49.         incr chmodTestsSkipped
  50.     }
  51.     return $stat
  52. }
  53.  
  54. global chmodTestsSkipped
  55. set chmodTestsSkipped 0
  56.  
  57. #-----------------------------------------------------------------------------
  58. # Procedure to return the uid of a file.
  59.  
  60. proc GetUID {file} {
  61.     file stat $file stat
  62.     return $stat(uid)
  63. }
  64.  
  65. #-----------------------------------------------------------------------------
  66. # Procedure to return the gid of a file.
  67.  
  68. proc GetGID {file} {
  69.     file stat $file stat
  70.     return $stat(gid)
  71. }
  72.  
  73. #-----------------------------------------------------------------------------
  74. # Procedure to return the uid and gid of a file.
  75.  
  76. proc GetUIDGID {file} {
  77.     file stat $file stat
  78.     return [list $stat(uid) $stat(gid)]
  79. }
  80.  
  81.  
  82. #-----------------------------------------------------------------------------
  83. # If a user does not have a group name assigned, then some tests will not work,
  84. # just blow off the tests and let the user make things right. 
  85.  
  86. if {[catch {id group}] != 0} {
  87.     echo "User '[id user]' does not have group name. Chmod tests skipped"
  88.     return
  89. }
  90.  
  91. #-----------------------------------------------------------------------------
  92. # Purge existing test files and recreate them.
  93. #
  94. proc SetUpTestFiles {} {
  95.     foreach f {CHECK.TMP CHMOD.TMP CHMOD2.TMP} {
  96.         unlink -nocomplain $f
  97.         close [open $f w]
  98.     }
  99. }
  100.  
  101. SetUpTestFiles
  102.  
  103. # Set the umask so that no bits are masked.  Some system chmods use umask
  104. # if u, g, o or a are not specified in a symbolic chmod.
  105.  
  106. umask 000
  107.  
  108. Test chmod-1.1 {chmod absolute mode tests} {
  109.     chmod 0000 CHMOD.TMP
  110.     chmod 0101 CHMOD.TMP
  111.     GetMode    CHMOD.TMP
  112. } 0 {101}
  113.  
  114. Test chmod-1.2 {chmod absolute mode tests} {
  115.     chmod 0000 CHMOD.TMP
  116.     chmod 0010 CHMOD.TMP
  117.     GetMode    CHMOD.TMP
  118. } 0 {10}
  119.  
  120. Test chmod-1.3 {chmod absolute mode tests} {
  121.     chmod 0000 CHMOD.TMP
  122.     chmod 0777 CHMOD.TMP
  123.     GetMode    CHMOD.TMP
  124. } 0 {777}
  125.  
  126. Test chmod-1.4 {chmod absolute mode tests} {
  127.     chmod 0000 CHMOD.TMP
  128.     chmod 0666 CHMOD.TMP
  129.     GetMode    CHMOD.TMP
  130. } 0 {666}
  131.  
  132. Test chmod-1.5 {chmod absolute mode tests} {
  133.     chmod 0000 CHMOD.TMP
  134.     chmod 0705 CHMOD.TMP
  135.     GetMode    CHMOD.TMP
  136. } 0 {705}
  137.  
  138. Test chmod-1.7 {chmod absolute mode tests} {
  139.     chmod  0000 CHMOD.TMP
  140.     chmod 04111 CHMOD.TMP
  141.     GetMode     CHMOD.TMP
  142. } 0 {4111}
  143.  
  144. Test chmod-2.1 {chmod absolute integer mode tests} {
  145.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  146.     chmod  65 {CHMOD.TMP CHMOD2.TMP}
  147.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  148. } 0 {101 101}
  149.  
  150. Test chmod-2.2 {chmod absolute integer mode tests} {
  151.     chmod 0 {CHMOD.TMP CHMOD2.TMP}
  152.     chmod 8 {CHMOD.TMP CHMOD2.TMP}
  153.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  154. } 0 {10 10}
  155.  
  156. Test chmod-2.3 {chmod absolute integer mode tests} {
  157.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  158.     chmod 511 {CHMOD.TMP CHMOD2.TMP}
  159.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  160. } 0 {777 777}
  161.  
  162. Test chmod-2.4 {chmod absolute integer mode tests} {
  163.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  164.     chmod 438 {CHMOD.TMP CHMOD2.TMP}
  165.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  166. } 0 {666 666}
  167.  
  168. Test chmod-2.5 {chmod absolute integer mode tests} {
  169.     chmod   0 {CHMOD.TMP CHMOD2.TMP}
  170.     chmod 453 {CHMOD.TMP  CHMOD2.TMP}
  171.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  172. } 0 {705 705}
  173.  
  174. Test chmod-2.6 {chmod absolute integer mode tests} {
  175.     chmod 0    CHMOD.TMP
  176.     chmod 2121 CHMOD.TMP
  177.     GetMode    CHMOD.TMP
  178. } 0 {4111}
  179.  
  180. # Test symbolic mode.
  181.  
  182. Test chmod-3.1 {chmod symbolic mode tests} {
  183.     chmod 000 CHMOD.TMP
  184.     chmod +r  CHMOD.TMP
  185.     GetMode   CHMOD.TMP
  186. } 0 {444}
  187.  
  188. Test chmod-3.2 {chmod symbolic mode tests} {
  189.     chmod 000 CHMOD.TMP
  190.     chmod +r  CHMOD.TMP
  191.     chmod +w  CHMOD.TMP
  192.     GetMode   CHMOD.TMP
  193. } 0 {666}
  194.  
  195. Test chmod-3.3 {chmod symbolic mode tests} {
  196.     chmod 000 CHMOD.TMP
  197.     chmod +r  CHMOD.TMP
  198.     chmod +w  CHMOD.TMP
  199.     chmod +x  CHMOD.TMP
  200.     GetMode   CHMOD.TMP
  201. } 0 {777}
  202.  
  203. Test chmod-3.4 {chmod symbolic mode tests} {
  204.     chmod 000 CHMOD.TMP
  205.     chmod +r  CHMOD.TMP
  206.     chmod +w  CHMOD.TMP
  207.     chmod +x  CHMOD.TMP
  208.     chmod -r  CHMOD.TMP
  209.     GetMode   CHMOD.TMP
  210. } 0 {333}
  211.  
  212. Test chmod-3.5 {chmod symbolic mode tests} {
  213.     chmod 000 CHMOD.TMP
  214.     chmod +r  CHMOD.TMP
  215.     chmod +w  CHMOD.TMP
  216.     chmod +x  CHMOD.TMP
  217.     chmod -r  CHMOD.TMP
  218.     chmod -w  CHMOD.TMP
  219.     GetMode   CHMOD.TMP
  220. } 0 {111}
  221.  
  222. Test chmod-3.6 {chmod symbolic mode tests} {
  223.     chmod 000 {CHMOD.TMP CHMOD2.TMP}
  224.     chmod +r  {CHMOD.TMP CHMOD2.TMP}
  225.     chmod +w  {CHMOD.TMP CHMOD2.TMP}
  226.     chmod +x  {CHMOD.TMP CHMOD2.TMP}
  227.     chmod -r  {CHMOD.TMP CHMOD2.TMP}
  228.     chmod -w  {CHMOD.TMP CHMOD2.TMP}
  229.     chmod -x  {CHMOD.TMP  CHMOD2.TMP}
  230.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  231. } 0 {0 0}
  232.  
  233. Test chmod-3.7 {chmod symbolic mode tests} {
  234.     chmod 000     CHMOD.TMP
  235.     chmod u+x,g+x CHMOD.TMP
  236.     GetMode       CHMOD.TMP
  237. } 0 {110}
  238.  
  239. Test chmod-3.8 {chmod symbolic mode tests} {
  240.     chmod 000     {CHMOD.TMP CHMOD2.TMP}
  241.     chmod u+x,g+x {CHMOD.TMP CHMOD2.TMP}
  242.     chmod u-x,g-x {CHMOD.TMP CHMOD2.TMP}
  243.     list [GetMode CHMOD.TMP] [GetMode CHMOD2.TMP]
  244. } 0 {0 0}
  245.  
  246. # Can't +s on some systems
  247.  
  248. if [CheckChmod "ugo+x,ug+s" "---s--s--x"] {
  249.     Test chmod-3.9 {chmod symbolic mode tests} {
  250.         chmod 000        CHMOD.TMP
  251.         chmod ugo+x,ug+s CHMOD.TMP
  252.         GetMode          CHMOD.TMP
  253.     } 0 {6111}
  254. }
  255.  
  256. Test chmod-3.10 {chmod symbolic mode tests} {
  257.     chmod 000   CHMOD.TMP
  258.     chmod a+rwx CHMOD.TMP
  259.     GetMode     CHMOD.TMP
  260. } 0 {777}
  261.  
  262. Test chmod-3.11 {chmod symbolic mode tests} {
  263.     chmod 000   CHMOD.TMP
  264.     chmod a+rwx CHMOD.TMP
  265.     chmod a-rw  CHMOD.TMP
  266.     GetMode     CHMOD.TMP
  267. } 0 {111}
  268.  
  269. Test chmod-3.12 {chmod symbolic mode tests} {
  270.     chmod 000   CHMOD.TMP
  271.     chmod a=rwx CHMOD.TMP
  272.     GetMode     CHMOD.TMP
  273. } 0 {777}
  274.  
  275. Test chmod-3.13 {chmod symbolic mode tests} {
  276.     chmod 000         CHMOD.TMP
  277.     chmod u=rwx,go=rx CHMOD.TMP
  278.     GetMode           CHMOD.TMP
  279. } 0 {755}
  280.  
  281.  
  282. # +t is dificult to test if not root, just make sure it execute and hope
  283. # for the best.  Doesn't even work on some systems
  284.  
  285. if [CheckChmod "u+t" "----------" "---------T"] {
  286.  
  287.     Test chmod-3.14 {chmod symbolic mode tests} {
  288.         chmod 000 CHMOD.TMP
  289.         chmod u+x CHMOD.TMP
  290.         chmod u+t CHMOD.TMP
  291.         set mode [GetMode CHMOD.TMP]
  292.         expr "($mode == 100) || ($mode == 1100)"
  293.     } 0 {1}
  294.  
  295.     Test chmod-3.14 {chmod symbolic mode tests} {
  296.         chmod 000 CHMOD.TMP
  297.         chmod u+x CHMOD.TMP
  298.         chmod u+t CHMOD.TMP
  299.     } 0 {}
  300.  
  301.     Test chmod-3.15 {chmod symbolic mode tests} {
  302.         chmod 000   CHMOD.TMP
  303.         chmod u+x   CHMOD.TMP
  304.         chmod u+t   CHMOD.TMP
  305.         chmod u-t   CHMOD.TMP
  306.     } 0 {}
  307. }
  308.  
  309. Test chmod-3.16 {chmod symbolic mode tests} {
  310.     chmod 000         CHMOD.TMP
  311.     chmod a+rwx       CHMOD.TMP
  312.     chmod u-r,g-w,o-x CHMOD.TMP
  313.     GetMode           CHMOD.TMP
  314. } 0 {356}
  315.  
  316. Test chmod-4.1 {chmod error tests} {
  317.     chmod +z CHMOD.TMP
  318. } 1 {invalid file mode "+z"}
  319.  
  320. Test chmod-4.2 {chmod error tests} {
  321.     chmod
  322. } 1 {wrong # args: chmod mode filelist}
  323.  
  324. # chown and chgrp tests
  325.  
  326. #
  327. # Some machines have problems with changing group ids on files (even to your
  328. # own) if you are not root. If thats the case, bail out here.
  329. #
  330.  
  331. SetUpTestFiles
  332.  
  333. if {[catch {chown [id userid]  CHMOD.TMP} msg ] ||
  334.     [catch {chgrp [id groupid] CHMOD.TMP} msg ]} {
  335.     puts stderr "*************************************************************"
  336.     puts stderr "Can't do chown or chgrp even when current and new group"
  337.     puts stderr "id are ours.  chown/chgrp tests skipped on this system."
  338.     puts stderr "    $msg"
  339.     puts stderr "*************************************************************"
  340.     unlink {CHECK.TMP CHMOD.TMP CHMOD2.TMP}
  341.     return
  342. }
  343.  
  344. set myUID [id userid]
  345. set myGID [id groupid]
  346.  
  347. Test chmod-5.1 {chown tests} {
  348.     SetUpTestFiles
  349.     chown [id user] {CHMOD.TMP CHMOD2.TMP}
  350.     list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP]
  351. } 0 [list $myUID $myUID]
  352.  
  353. Test chmod-5.2 {chown tests} {
  354.     chown [id userid] {CHMOD.TMP CHMOD2.TMP}
  355.     list [GetUID CHMOD.TMP] [GetUID CHMOD2.TMP]
  356. } 0 [list $myUID $myUID]
  357.  
  358. Test chmod-5.3 {chown tests} {
  359.     chown [list [id userid] [id groupid]] {CHMOD.TMP CHMOD2.TMP}
  360.     list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP]
  361. } 0 [list [list $myUID $myGID] [list $myUID $myGID]]
  362.  
  363. Test chmod-5.4 {chown tests} {
  364.     chown [list [id user] [id group]] {CHMOD.TMP CHMOD2.TMP}
  365.     list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP]
  366. } 0 [list [list $myUID $myGID] [list $myUID $myGID]]
  367.  
  368. Test chmod-5.5 {chown tests} {
  369.     chown [list [id user] [id group]] {CHMOD.TMP CHMOD2.TMP}
  370.     list [GetUIDGID CHMOD.TMP] [GetUIDGID CHMOD2.TMP]
  371. } 0 [list [list $myUID $myGID] [list $myUID $myGID]]
  372.  
  373. SetUpTestFiles
  374.  
  375. Test chmod-6.1 {chown error tests} {
  376.     chown XXXXXXXXX CHMOD.TMP
  377. } 1 {unknown user id: XXXXXXXXX}
  378.  
  379. Test chmod-6.2 {chown error tests} {
  380.     chown [list XXXXXXXXX [id groupid]] CHMOD.TMP
  381. } 1 {unknown user id: XXXXXXXXX}
  382.  
  383. Test chmod-6.3 {chown error tests} {
  384.     chown [list [id user] XXXXXXXXX] CHMOD.TMP
  385. } 1 {unknown group id: XXXXXXXXX}
  386.  
  387. Test chmod-6.4 {chown error tests} {
  388.     chown {XXXXXXXXX YYYY} CHMOD.TMP
  389. } 1 {unknown user id: XXXXXXXXX}
  390.  
  391. Test chmod-6.5 {chown error tests} {
  392.     chown
  393. } 1 {wrong # args: chown user|{user group} filelist}
  394.  
  395. Test chmod-7.1 {chgrp tests} {
  396.     chgrp [id group]  {CHMOD.TMP CHMOD2.TMP}
  397.     list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP]
  398. } 0 [list $myGID $myGID]
  399.  
  400. Test chmod-7.2 {chgrp tests} {
  401.     chgrp [id groupid] {CHMOD.TMP CHMOD2.TMP}
  402.     list [GetGID CHMOD.TMP] [GetGID CHMOD2.TMP]
  403. } 0 [list $myGID $myGID]
  404.  
  405. Test chmod-8.1 {chgrp error tests} {
  406.     chgrp
  407. } 1 {wrong # args: chgrp group filelist}
  408.  
  409. Test chmod-8.2 {chgrp error tests} {
  410.     chgrp XXXXXXXXX CHMOD.TMP
  411. } 1 {unknown group id: XXXXXXXXX}
  412.  
  413. unlink {CHECK.TMP CHMOD.TMP CHMOD2.TMP}
  414.  
  415. # If this is a TclX development system, then give a warning about skipped
  416. # tests.
  417.  
  418. if {[file exists RCS] && ($chmodTestsSkipped > 0)} {
  419.     puts stderr "WARNING: $chmodTestsSkipped chmod tests skipped due to system strangeness"
  420. }
  421.